<template>
  <div>
    <el-table
      class="table"
      :cols="cols"
      v-loading="loading"
      :row-key="rowKey"
      :data="data"
      border
    >
      <el-table-column
        :label="col.title"
        :prop="col.name"
        :type="col.type"
        :width="col.width"
        :align="col.align"
        v-for="(col, index) in cols"
        :key="index"
      >
        <template v-if="col.slot" v-slot="{ row, index }">
          <slot
            :row="row"
            :index="index"
            v-if="col.slot"
            :name="col.slot"
          ></slot>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-Pagination
      class="page"
      v-if="value"
      @size-change="(v) => change('pagesize', v)"
      @current-change="(v) => change('pagenum', v)"
      :current-page="value.pagenum"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :page-size="value.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-Pagination>
  </div>
</template>

<script>
export default {
  name: "MyTable",
  methods: {
    /**
     * 分页或页码更改事件
     */
    change(key, value) {
      const obj = { ...this.value, [key]: value };
      this.$emit("input", obj);
      this.$emit("change", obj);
    },
  },
  props: {
    /**
     * loading
     */
    loading: Boolean,
    /**
     * 列
     */
    cols: {
      required: true,
      type: Array,
      default: () => [],
    },
    /**
     * 数据
     */
    data: {
      required: true,
      type: Array,
      default: () => [],
    },
    /**
     * 分页对象（传入则开启分页）
     */
    value: Object,
    /**
     * 总条数
     */
    total: Number,
    /**
     * rowkey
     */
    rowKey: {
      type: String,
      default: "id",
    },
  },
};
</script>

<style  scoped>
.page {
  margin-top: 20px;
}
</style>